package org.bob.learn.web.controller;

import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.bob.learn.dao.entity.Student;
import org.bob.learn.service.StudentService;
import org.bob.learn.web.model.Result;
import org.bob.learn.web.model.StudentCriteria;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {

    private final StudentService studentService;

    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }

    /**
     * 根据标识查询学生信息
     * @param id 学生标识
     * @return 学生信息
     */
    @GetMapping("query")
    @Operation(summary = "根据标识查询学生信息")
    Mono<Result<Student>> queryById(@Param(value = "id") Long id){
        /*
          响应式编成
          空时返回默认元素
         */
        return studentService.queryById(id).map(Result::Success).defaultIfEmpty(Result.Success(Student.class));
    }

    /**
     * 根据复合条件查询学生信息列表
     * 复合条件：多字段+分页
     * @param criteria 复合查询条件
     * @return 学生信息列表
     */
    @PostMapping("queryByCriteria")
    Mono<Result<List<Student>>> queryByCriteria(@RequestBody StudentCriteria criteria){
        return studentService.queryByCriteria(criteria).collectList().map(Result::Success);
    }
}
